package com.jprocms.module.cms.controller.admin.vote;

import com.jpro.framework.common.pojo.CommonResult;
import com.jpro.framework.common.pojo.PageResult;
import com.jpro.framework.excel.core.util.ExcelUtils;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.jprocms.module.cms.controller.admin.vote.vo.*;
import com.jprocms.module.cms.convert.vote.VoteConvert;
import com.jprocms.module.cms.dal.dataobject.vote.VoteDO;
import com.jprocms.module.cms.service.vote.VoteService;
import com.jprocms.module.cms.util.CmsContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import static com.jpro.framework.common.pojo.CommonResult.success;
import static com.jprocms.module.cms.web.Directives.getOrderItems;

@Tag(name = "管理后台 - 投票")
@RestController
@RequestMapping("/cms/vote")
@Validated
public class VoteController {

    @Resource
    private VoteService voteService;

    @PostMapping("/create")
    @Operation(summary = "创建投票")
    @PreAuthorize("@ss.hasPermission('cms:vote:create')")
    public CommonResult<Long> createVote(@Valid @RequestBody VoteCreateReqVO createReqVO) {
        createReqVO.setSiteId(CmsContextUtil.getCurrSiteId());
        createReqVO.init();
        return success(voteService.createVote(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新投票")
    @PreAuthorize("@ss.hasPermission('cms:vote:update')")
    public CommonResult<Boolean> updateVote(@Valid @RequestBody VoteUpdateReqVO updateReqVO) {
        voteService.updateVote(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除投票")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cms:vote:delete')")
    public CommonResult<Boolean> deleteVote(@RequestParam("id") Long id) {
        voteService.deleteVote(id);
        return success(true);
    }

    @DeleteMapping("/deleteByIds")
    @Operation(summary = "批量删除投票")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cms:vote:delete')")
    public CommonResult<Boolean> deleteByIds(@RequestBody List<Long> ids) {
        voteService.deleteByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得投票")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cms:vote:query')")
    public CommonResult<VoteRespVO> getVote(@RequestParam("id") Long id) {
        VoteDO vote = voteService.getVote(id);
        return success(VoteConvert.INSTANCE.convert(vote));
    }

    @GetMapping("/list")
    @Operation(summary = "获得投票列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('cms:vote:query')")
    public CommonResult<List<VoteRespVO>> getVoteList(@RequestParam("ids") Collection<Long> ids) {
        List<VoteDO> list = voteService.getVoteList(ids);
        return success(VoteConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得投票分页")
    @PreAuthorize("@ss.hasPermission('cms:vote:query')")
    public CommonResult<PageResult<VoteRespVO>> getVotePage(@Valid VotePageReqVO pageVO) {
        pageVO.setSiteId(CmsContextUtil.getCurrSiteId());
        //排序值降序，ID降序排序
        Collection<OrderItem> orderItems = getOrderItems(Arrays.asList("vote_sort_desc", "id_desc"));
        pageVO.setOrderItems(orderItems);
        PageResult<VoteDO> pageResult = voteService.getVotePage(pageVO);
        return success(VoteConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出投票 Excel")
    @PreAuthorize("@ss.hasPermission('cms:vote:export')")

    public void exportVoteExcel(@Valid VoteListReqVO exportReqVO,
                                HttpServletResponse response) throws IOException {
        //排序值降序，ID降序排序
        Collection<OrderItem> orderItems = getOrderItems(Arrays.asList("vote_sort_desc", "id_desc"));
        exportReqVO.setOrderItems(orderItems);
        List<VoteDO> list = voteService.getVoteList(exportReqVO);
        // 导出 Excel
        List<VoteExcelVO> datas = VoteConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "投票.xls", "数据", VoteExcelVO.class, datas);
    }

}
